package generator;

import org.eclipse.uml2.uml.Element;
import java.util.Properties;

public class JpaEntityAccessorsTemplateClass extends BaseTemplateClass {


	public JpaEntityAccessorsTemplateClass(Properties properties) {
		super(properties);
	}

	public boolean isProcessable() {
		return true;
	}

	public void setElement(Element ele) {
		this.element = ele;
		returningString = new StringBuilder();
	}

	protected String _returning() {
		returningString.append("\n\t/**\n\t * JPA configuration for the \'");
		returningString.append(getPropName());
		returningString.append("\' attribute for its persistence.\n\t */\n\t@Basic\n\tprivate ");
		returningString.append(getPropyTypeName());
		returningString.append(" ");
		returningString.append(getPropName());
		returningString.append(";\n\n\t/**\n\t * Retorna el valor de ");
		returningString.append(getPropName());
		returningString.append(".\n\t * @return\n\t */\n\tpublic ");
		returningString.append(getPropyTypeName());
		returningString.append(" get");
		returningString.append(getMethodName());
		returningString.append("() {\n\t\treturn this.");
		returningString.append(getPropName());
		returningString.append(";\n\t}\n\n\t/**\n\t * Permite modificar el valor de ");
		returningString.append(getPropName());
		returningString.append(".\n\t * @param ");
		returningString.append(getPropName());
		returningString.append("\n\t */\n\tpublic void set");
		returningString.append(getMethodName());
		returningString.append("(");
		returningString.append(getPropyTypeName());
		returningString.append(" ");
		returningString.append(getPropName());
		returningString.append(") {\n\t\tthis.");
		returningString.append(getPropName());
		returningString.append(" = ");
		returningString.append(getPropName());
		returningString.append(";\n\t}\n\t");

		return returningString.toString();
	}
	
	private String propName;
	
	private String getPropName() {
		return propName;
	}
	
	private String propTypeName;
	
	private String getPropyTypeName() {
		return propTypeName;
	}
	
	private String methodName;
	
	private String getMethodName() {
		return methodName;
	}
	
	/**
	 * Genera los getteres y setters de la clase configurados para su
	 * persistencia con JPA.
	 * Este método utiliza el estándar "returning()"
	 * 
	 * @return
	 */
	public String returning() {
		org.eclipse.uml2.uml.Class cls = (org.eclipse.uml2.uml.Class)element;
		StringBuilder sb = new StringBuilder();
		for (org.eclipse.uml2.uml.Property prop : cls.getAttributes()) {
			propName = firstLower(prop.getName());
			methodName = firstUpper(prop.getName());
			propTypeName = translateDataType(prop.getType());
			sb.append(_returning());
		}
		return sb.toString();
	}

	/**
	 * Traduce los tipos de datos a la plataforma específica. Particulamente, si
	 * el tipo es DataType, realiza una conversión.
	 * 
	 * @param type
	 * @return
	 */
	private String translateDataType(org.eclipse.uml2.uml.Type type) {
		String propTypeName = type.getName();
		if (type instanceof org.eclipse.uml2.uml.DataType) {
			// según el tipo lo traduzco a la plataforma AppEngine
			if (propTypeName.equalsIgnoreCase("Date")) {
				propTypeName = "java.util.Date";
			} else if (propTypeName.equalsIgnoreCase("SystemUser")) {
				propTypeName = "com.google.appengine.api.users.User";
			} else if (propTypeName.equalsIgnoreCase("Binary")) {
				propTypeName = "byte[]"; //$NON-NLS-1$
			}
		}
		return propTypeName;
	}
	
}